package solutions.leetcode.easy;

import solutions.lib.BaseSolution;

/**
 * @author lizhidong
 * <a href="https://leetcode-cn.com/problems/surface-area-of-3d-shapes/">892. 三维形体的表面积</a>
 */
public class Solution892 extends BaseSolution {

    public int surfaceArea(int[][] grid) {
        int rst = 0;
        for (int i = 0; i < grid.length; i++) {
            for (int j = 0; j < grid[i].length; j++) {
                int now = grid[i][j];
                if (now == 0) {
                    continue;
                }
                rst += 2;
                int left = j == 0 ? now : Math.max(0, now - grid[i][j - 1]);
                int right = j == grid[i].length - 1 ? now : Math.max(0, now - grid[i][j + 1]);
                int top = i == 0 ? now : Math.max(0, now - grid[i - 1][j]);
                int bottom = i == grid.length - 1 ? now : Math.max(0, now - grid[i + 1][j]);
                rst += left + right + bottom + top;
            }
        }
        return rst;
    }

    public static void main(String[] args) {
        Solution892 solution = new Solution892();
        System.out.println(solution.surfaceArea(new int[][]{{1,0},{0,2}}));
    }
}